package com.hbnu;
import java.util.List;
import java.util.Scanner;

public class WeatherApp {
    private WeatherDAO weatherDAO = new WeatherDAOImpl();
    private Scanner scanner = new Scanner(System.in);

    public void start() {
        while (true) {
            System.out.println("\n===== 天气数据管理系统 =====");
            System.out.println("1. 添加天气记录");
            System.out.println("2. 查询天气记录");
            System.out.println("3. 删除天气记录");
            System.out.println("4. 显示所有天气记录");
            System.out.println("0. 退出");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addWeather();
                    break;
                case 2:
                    queryWeather();
                    break;
                case 3:
                    deleteWeather();
                    break;
                case 4:
                    displayAllWeather();
                    break;
                case 0:
                    System.out.println("系统已退出。");
                    return;
                default:
                    System.out.println("无效输入，请重新选择！");
            }
        }
    }

    private void addWeather() {
        System.out.print("输入城市: ");
        String city = scanner.nextLine();
        System.out.print("输入温度(°C): ");
        double temperature = scanner.nextDouble();
        System.out.print("输入湿度(%): ");
        int humidity = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        System.out.print("输入日期(YYYY-MM-DD): ");
        String date = scanner.nextLine();

        Weather weather = new Weather(city, temperature, humidity, date);
        weatherDAO.addWeather(weather);
        System.out.println("天气记录已添加！");
    }

    private void queryWeather() {
        System.out.print("输入要查询的城市: ");
        String city = scanner.nextLine();
        List<Weather> result = weatherDAO.getWeatherByCity(city);

        if (result.isEmpty()) {
            System.out.println("未找到该城市的天气记录！");
        } else {
            System.out.println("查询结果:");
            for (Weather weather : result) {
                System.out.println(weather);
            }
        }
    }

    private void deleteWeather() {
        System.out.print("输入要删除的城市: ");
        String city = scanner.nextLine();
        boolean deleted = weatherDAO.deleteWeatherByCity(city);
        if (deleted) {
            System.out.println("已删除该城市的所有天气记录！");
        } else {
            System.out.println("未找到该城市的天气记录！");
        }
    }
    private void displayAllWeather() {
        // 1. 通过DAO获取所有天气记录
        List<Weather> allWeather = weatherDAO.getAllWeather();

        // 2. 展示结果
        if (allWeather.isEmpty()) {
            System.out.println("暂无天气数据！");
        } else {
            // 遍历所有记录并打印
            for (Weather weather : allWeather) {
                System.out.println(weather);
            }
        }
    }

}